<?php
namespace app\api\controller;
header('Access-Control-Allow-Origin:*');
import('app.api.extend.wechat.MessagePush');
import('app.api.extend.wechat.JsApi');
use think\Controller;
use think\Db;
use think\Request;

/**
 *
 * @group_name
 *
 * @author Master
 *
 * @api ${API}
 *
 * @time 2020/6/3 - 13:00
 *
 */

class CarServer extends Controller{

    //获取品牌列表
    public function get_car_brandlist(){
        //根据客户端传过来的page获取品牌列表
        $page = input("page");
        $data =  Db::name("carinfo")->where(["data_state" =>1])->limit(20)->page($page)->select();
        $data = [
            "status" =>200,
            "msg" =>"获取成功",
            "item" => $data
        ];
        return $data;

    }

    //根据车辆品牌ID查询旗下车系列表
    public function get_car_level(){
        $request = Request::instance();
        $data = $request->param();
        $result = Db::name("carseries")->where(["data_state" =>1,"brandid" => $data["brandid"]])->select();
        $data = [
            "status" =>200,
            "msg" =>"获取成功",
            "item" => $result
        ];
        return $data;
   }



    //根据车系id查询旗下车型列表
    public function get_car_models(){
        $request = Request::instance();
        $data = $request->param();
        $result = Db::name("car_models")->where(["data_state" =>1,"series_id" => $data["series_id"]])->select();
        $data = [
            "status" =>200,
            "msg" =>"获取成功",
            "item" => $result
        ];
        return $data;
    }



    public function add_car(){
        $request = Request::instance();
        $data = $request->param();
    }


    public function reserve(){
  
        // reserve    id username telephone model province city dealer data_state  create_time update_time status
        // send_msg_log  msg  code   id username telephone create_time update_time status data_state
        $request = Request::instance();
        $data = $request->param();
        if (empty($data["telephone"])){
            if (!$this->check_phone_num($data["telephone"])){
                $data = [
                    "status" =>0,
                    "msg" =>"请输入正确的手机号",
                    "item" => []
                ];
                return $data;
            }
            $data = [
                "status" =>0,
                "msg" =>"请输入手机号",
                "item" => []
            ];
            return $data;
        }

        if (empty($data["username"])){
            $data = [
                "status" =>0,
                "msg" =>"请输入姓名",
                "item" => []
            ];
            return $data;
        }
        if (empty($data["code"])){
            $data = [
                "status" =>0,
                "msg" =>"请输入验证码",
                "item" => []
            ];
            return $data;
        }
        $check_user = Db::name("reserve")->where(["telephone" => $data["telephone"],"data_state" => 1])->find();
        if ($check_user){
            $data = [
                "status" =>0,
                "msg" =>"您已经预约了",
                "item" => []
            ];
            return $data;
        }
        $check_code = Db::name("send_msg_log")->where(["telephone" => $data["telephone"],"code" => $data["code"],"data_state" => 1,"status" => 1])->find();
        if (!$check_code){
            $data = [
                "status" =>0,
                "msg" =>"验证码错误",
                "item" => []
            ];
            return $data;
        }else{
                    $insert_data=[
                        "username"=> $data["username"],
                        "telephone"=> $data["telephone"],
                        "model"=> $data["model"],
                        "province"=> $data["province"],
                        "city"=> $data["city"],
                        "dealer"=> $data["dealer"],
                        "data_state"=> 1,
                        "create_time"=> date("Y-m-d H:i:s", time()),
                        "update_time"=> date("Y-m-d H:i:s", time()),
                        "status"=> 1,
                    ];
                    $reserve = Db::name("reserve")->insertGetId($insert_data);
                    Db::name("send_msg_log")->where(["id" => $check_code["id"]])->update(["status" => 0]);
                    if ($reserve){
                        $data = [
                            "status" =>1,
                            "msg" =>"预约成功",
                            "item" => []
                        ];
                        return $data;
                    }else{
                        $data = [
                            "status" =>0,
                            "msg" =>"预约失败，请稍后重试",
                            "item" => []
                        ];
                        return $data;
                    }

        }

    }

    public function jsapistest()
    {

        $request = Request::instance();
        $data = $request->param();
        $jsapi = new \JsApi();
        $sign  = $jsapi->getSignPackage1($data["url"]);
        $this->assign('signPackage', $sign);
        $this->data["data"] = $sign;
        return $this->data["data"];
    }
    public function jsapistest1()
    {

        $request = Request::instance();
        $data = $request->param();
        $jsapi = new \JsApi();

        $sign  = $jsapi->getSignPackage2($data["requesturl"]);
        $this->assign('signPackage', $sign);
        $this->data["data"] = $sign;
        return $this->data["data"];
    }

    public function send_msg(){
        header('Access-Control-Allow-Origin:*');
        $request = Request::instance();
        $data = $request->param();
        if (empty($data["telephone"])){
            if (!$this->check_phone_num($data["telephone"])){
                $data = [
                    "status" =>0,
                    "msg" =>"请输入正确的手机号",
                    "item" => []
                ];
                return $data;
            }
            $data = [
                "status" =>0,
                "msg" =>"请输入手机号",
                "item" => []
            ];
            return $data;
        }
        $result = $this->sen_msg($data["telephone"]);
        if ($result){
            $insert_data= [
                "telephone" =>$result["mobile"],
                "code" => $result["code"],
                "status" => 1,
                "data_state" => 1,
                "create_time"=> date("Y-m-d H:i:s", time()),
                "update_time"=> date("Y-m-d H:i:s", time()),
                "msg" => $result["msg"]
            ];
            $check_data = Db::name("send_msg_log")->insert($insert_data);
            if ($check_data){
                $data = [
                    "status" =>1,
                    "msg" =>"发送成功",
                    "item" => []
                ];
                return $data;
            }else{
                $data = [
                    "status" =>0,
                    "msg" =>"发送失败，请稍后再试",
                    "item" => []
                ];
                return $data;
            }

        }else{
            $data = [
                "status" =>0,
                "msg" =>"发送失败，请稍后再试",
                "item" => []
            ];
            return $data;
        }
    }

    function check_phone_num($phone_num) {
        if (preg_match("/^1((34[0-8]\d{7})|((3[0-3|5-9])|(4[5-7|9])|(5[0-3|5-9])|(66)|(7[2-3|5-8])|(8[0-9])|(9[1|8|9]))\d{8})$/", $phone_num)) {
            return true;
        } else {
            return false;
        }
    }
    public function sen_msg($mobile)
    {

        header("Content-type: text/html; charset=utf-8");
        $account = "9M00072";    //改为实际账户名
        $password = "123456";    //改为实际短信发送密码
        //目标手机号码，多个用半角“,”分隔
        $extno = "";
        $code = rand(100000, 999999);
        $content = "【广汽三菱】您的验证码是：".$code."。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        $sendtime = date("Y-m-d H:i:s", time());
        $result = $this->send1($account, $password, $mobile, $extno, $content, $sendtime);
        $xml = simplexml_load_string($result);
        $result1 = json_encode($xml);

        $xml = simplexml_load_string($result);
        if($xml->returnstatus=="Faild")
        {
            // 打印出错信息
            return false;
        }
        else
        {
            $result = [
                "send_time" => $sendtime,
                "code" => $code,
                "msg" => $content,
                "mobile" => $mobile,
            ];
            return $result ;
        }

    }
    public function send1($account, $password, $mobile, $extno, $content, $sendtime)
    {
        $url = "https://dx.ipyy.net/sms.aspx";
        $body = array(
            'action' => 'send',
            'userid' => '',
            'account' => $account,
            'password' => $password,
            'mobile' => $mobile,
            'extno' => $extno,
            'content' => $content,
            'sendtime' => $sendtime
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public function test(){
        $result = Db::name("teams")->select();
        $data = [
            "status" =>1,
            "msg" =>"获取成功",
            "item" => $result
        ];
        return $data;
    }



}